package com.t4.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t4.auth.service.SysUserService;
import com.t4.common.result.Result;
import com.t4.common.utils.MD5;
import com.t4.model.system.SysUser;
import com.t4.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * id查询角色
     * @param id
     * @return
     */
    @ApiOperation(value = "id查询用户")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id){
        SysUser sysUser = sysUserService.getById(id);
        return Result.success(sysUser);
    }

    /**
     * 用户条件分页查询
     * @param current 当前页
     * @param size 每页限制条数
     * @param sysUserQueryVo 查询的条件对象
     * @return
     */
    @ApiOperation(value = "用户条件分页查询")
    @GetMapping("/{current}/{size}")
    public Result pageQueryUser(@PathVariable Long current,
                                @PathVariable Long size,
                                SysUserQueryVo sysUserQueryVo){
        Page<SysUser> pageParam = new Page<>(current, size);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        String keyword = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        // 模糊查询
        if(!StringUtils.isEmpty(keyword)){
            queryWrapper.like(SysUser::getUsername, keyword);
        }
        // 在设定时间之内
        if(!StringUtils.isEmpty(createTimeBegin)){
            queryWrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            queryWrapper.le(SysUser::getCreateTime, createTimeEnd);
        }
        sysUserService.page(pageParam, queryWrapper);
        return Result.success(pageParam);
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser){
        // 使用 MD5 对密码进行加密
        String passwordMD5 = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(passwordMD5);
        boolean is_success = sysUserService.save(sysUser);
        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "修改用户")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysUser sysUser){

        boolean is_success = sysUserService.updateById(sysUser);


        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        boolean is_success = sysUserService.removeById(id);
        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "更改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status){
        boolean is_success = sysUserService.updateStatus(id, status);
        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "获取当前用户基本信息")
    @GetMapping("getCurrentUser")
    public Result getCurrentUser() {
        Map<String, Object> map = sysUserService.getCurrentUser();
        return Result.success(map);
    }

}
